package com.zc.mychart.model;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.zc.mychart.utils.DecimalUtil;

import java.util.ArrayList;
import java.util.List;

public class SKDJEntrys {

    private List<Entry> Ks;
    private List<Entry> Ds;

    private final int N = 9;
    private final int M = 3;

    private List<Float> rsvList;
    private List<Float> mDList;
    private List<Float> mRsvList;

    public SKDJEntrys(List<CandleEntry> list) {
        Ks = new ArrayList<>();
        Ds = new ArrayList<>();
        rsvList = new ArrayList<>();
        mDList = new ArrayList<>();
        mRsvList = new ArrayList<>();
        float k = 50.0f;
        float d = 50.0f;
        boolean drawGreen;
        float rsv = 0;
        float lastrsv = 0;
        if (list != null && list.size() > 0) {
            float high;
            float low;
            for (int i = N - 1; i < list.size(); i++) {
                Float[] wrs = getHighAndLowByK(i, list);
                high = wrs[0];
                low = wrs[1];
                if (high != low) {
                    rsv = (list.get(i).getClose() - low) / (high - low) * 100;
                } else {
                    rsv = 0;
                }

                rsv = (float) DecimalUtil.round(rsv, 4);

                if (i == N - 1) {
                    lastrsv = rsv;
                } else {
                    lastrsv = (2 * rsv + (M - 1) * lastrsv) / (M + 1);
                }

                mRsvList.add(rsv);
                rsvList.add(lastrsv);
            }

            List<Float> mEMAK = getEMAK(rsvList);
            mDList = getMAD(mEMAK);
            for (int j = 0; j < mEMAK.size(); j++) {
                CandleEntry entry = list.get(N + j - 1);
                k = mEMAK.get(j);
                if(j >= M){
                    d = mDList.get(j - M);
                    Ds.add(new Entry(entry.getX(),d));
                }
                drawGreen = !(k >= d);
                Ks.add(new Entry(entry.getX(), k, drawGreen));
            }
        }
    }

    /**
     * 得到某区间内最高价和最低价
     *
     * @param end 结束位置
     */
    private Float[] getHighAndLowByK(int end, List<CandleEntry> list) {
        CandleEntry candleEntry = list.get(end);
        float high = candleEntry.getHigh();
        float low = candleEntry.getLow();
        Float[] wrs = new Float[2];
        for (int j = 0; j < N; j++) {
            candleEntry = list.get(end - j);
            high = high > candleEntry.getHigh() ? high : candleEntry.getHigh();
            low = low < candleEntry.getLow() ? low : candleEntry.getLow();
        }
        wrs[0] = high;
        wrs[1] = low;
        return wrs;
    }


    public List<Float> getEMArsv(List<Float> list) {
        List<Float> rsvList = new ArrayList<>();
        float ema = 0;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                float rsv = list.get(i);
                if (i == 0) {
                    ema = rsv;
                } else {
                    ema = (2 * rsv + (M - 1) * ema) / (M + 1);
                }
                rsvList.add(ema);
            }
        }
        return rsvList;
    }


    public List<Float> getEMAK(List<Float> list) {
        List<Float> p = new ArrayList<>();
        float ema = 0;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                float rsv = list.get(i);
                if (i == 0) {
                    ema = rsv;
                } else {
                    ema = (2 * rsv + (M - 1) * ema) / (M + 1);
                }
                p.add(ema);
            }
        }
        return p;
    }


    public List<Float> getMAD(List<Float> list) {
        if (list != null) {
            for (int i = M; i < list.size(); i++) {
                float sum = 0;
                for (int j = 0; j < M; j++) {
                    sum += list.get(i - j);
                }
                mDList.add(sum / M);
            }
        }
        return mDList;
    }

    public List<Entry> getKs() {
//        for (int i = 0; i < mRsvList.size(); i++) {
//            Log.e("art", "index : " + i + ", rsv : " + mRsvList.get(i));
//        }

//        for (int i = 0; i < Ks.size(); i++) {
//            Log.e("art", "index : " + i + ", K : " + Ks.get(i));
//        }
        return Ks;
    }

    public List<Entry> getDs() {
        return Ds;
    }

}
